#!/bin/bash
#
# MikoPBX - free phone system for small business
# Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
#

echo " - Start ${0}";
delta=1; i=0;
freeOffloadFile='/tmp/freeoffload';

while : ; do

    if [ ! -t 0 ] || [ ! -t 1 ] ; then
        # Not a TTY, disable the menu.
        exit 2;
    fi
    if [ "$i" -gt 3 ]; then
        # System installation, disable the menu.
        exit 1;
    fi

    if [ "$delta" = "0" ]; then
        # System installation, disable the menu.
        i=$((i + 1));
    else
        i=0;
    fi
    start_time=$(date +%s);

    if [ -f /tmp/ejectcd ]; then
        # System installation, disable the menu.
        sleep 20;
    fi

    if [ -f /tmp/rebooting ]; then
        echo "Reboot pending (${0}). Cancel with rm /tmp/rebooting"
        # Waiting timeout
        WAIT_TIME=10
        counter=0

        while [ $counter -lt $WAIT_TIME ]; do
            if [ ! -f /tmp/rebooting ]; then
                # Flag file was deleted
                echo "System reboot was cancelled"
                touch /tmp/start_sh
                exit 0
            fi
            sleep 1
            counter=$((counter+1))
        done

        echo "Reboot the system..."
        /sbin/pbx_reboot;

        # Wait for reboot for 10 seconds.
        # dd if=/dev/urandom bs=1 count=10 2>/dev/null | pv -pL 1 | dd of=/dev/null 2>/dev/null;
    fi
    if [ -f /tmp/shutdown ]; then
        echo "Waiting for a shutdown (${0})";
        /sbin/shutdown;
        # Wait for reboot for 10 seconds.
        # dd if=/dev/urandom bs=1 count=10 2>/dev/null | pv -pL 1 | dd of=/dev/null 2>/dev/null;
    fi

    # Check if shell needs to be started.
    if [ -f /tmp/start_sh ]; then
        clear;
        rm -rf /tmp/start_sh;
        if [ -f /etc/debian_version ] ; then
            /bin/bash -login;
        elif [ "${SSH_CLIENT}x" = 'x' ] ; then
             # Redirect input and output to the console.
            exec < /dev/console > /dev/console  2>&1
            exec /bin/bash -login;
        else
            exec /bin/bash -login;
        fi
    fi
    # Ensure that the /offload partition is mounted.
    if [ ! -f "$freeOffloadFile" ]; then
        # Console menu depends on the /offload partition.
        /etc/rc/console_menu 2>/dev/null;
        resultMenu=$?;

        # Check for reboot.
        tput reset; tput civis;
        while [ -f "$freeOffloadFile" ]; do
            # Wait until reboot process finishes.
            sleep 1;
        done;
        tput cnorm;
        echo;
        if [ $resultMenu -gt 0 ]; then
            sleep 5;
            /bin/sh;
        fi;
    else
        sleep 5;
    fi

    end_time=$(date +%s);
    delta=$((end_time - start_time));
done